package com.mixue.controller;


import com.mixue.model.Employee;
import com.mixue.model.Goods;
import com.mixue.restful.ResoutSet;
import com.mixue.service.GoodsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/page")
    public ResoutSet<Page> page(int page, int pageSize, String name){

        Page<Goods> pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Goods> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.like(name!=null,Goods::getName,name);
        queryWrapper.orderByDesc(Goods::getType);
        Page<Goods> page1 = goodsService.page(pageInfo, queryWrapper);
        return ResoutSet.sussess(pageInfo);
    }
    @GetMapping("{id}")
    public ResoutSet<Goods> getById(@PathVariable Long id){
        Goods goods = goodsService.getById(id);
        System.out.println(goods);
        return ResoutSet.sussess(goods);
    }
    @GetMapping("/list")
    public ResoutSet<List> list(Goods goods){
//        List<Goods> goodsList=null;
//        String key="goods"+goods.getType();
//        goodsList= (List<Goods>) redisTemplate.opsForValue().get(key);

//        if (goodsList!=null){
//            System.out.println("从缓存查出数据");
//            return ResoutSet.sussess(goodsList);
//        }
//        后续待优化
        LambdaQueryWrapper<Goods> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(goods.getType()!=null,Goods::getType,goods.getType());
        queryWrapper.orderByDesc(Goods::getSort);
        List<Goods> list=goodsService.list(queryWrapper);
//        redisTemplate.opsForValue().set(key,list,10,TimeUnit.MINUTES);
        System.out.println("商品类型已加载");
        return ResoutSet.sussess(list);

    }

    @PostMapping
    public ResoutSet<String> add(@RequestBody Goods goods) {
        System.out.println(goods);
        goodsService.save(goods);
        return ResoutSet.sussess("添加成功！");
    }
    @PostMapping("/goods/status/{status}/{id}")
    public ResoutSet<String> status(@PathVariable Integer status,@PathVariable Long id) {
        LambdaQueryWrapper<Goods> queryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Goods> list = queryWrapper.select(Goods::getId);

        return ResoutSet.sussess("修改成功！");
    }
    @DeleteMapping
    public ResoutSet<String> delete(String id){
       goodsService.removeById(id);
       return ResoutSet.sussess("删除成功");
    }
    @PutMapping
    public ResoutSet<String> update(@RequestBody Goods goods){
        goodsService.updateById(goods);
        return ResoutSet.sussess("修改成功！");
    }
}
